/*
 *  Copyright 2015-2016, RenGu Technology (BeiJing) Co., Ltd.
 *  All right reserved.
 *
 *  THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF RENGU TECHNOLOGY (BeiJing) CO., LTD.
 *
 *  THE CONTENTS OF THIS FILE MAY NOT BE DISCLOSED TO THIR PARTIES,
 *  COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART,
 *  WITHOUT THE PRIOR WRITTEN PERMISSION OF RENGU TECHNOLOGY (BeiJing) CO., LTD.
 *
 *  Edit History:
 *      wukefan Created on 2020年08月19日
 *
 */
package cc.rengu.igas.cops.core.model;

import cc.rengu.igas.share.common.constant.ShareParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSONObject;

/**
 * 专用附加信息处理
 *
 * @author lizhangtao
 * @version 1.0
 * @date 2020/8/19
 */
public class ProcAdditionInfoImpl implements ProcAddition {

    @Override
    public String procAdditionInfo(String instId, String additionInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.INST_ID, instId);

        JSONObject jsonObject;
        if (!StringUtil.isEmptyOrNull(additionInfo)) {
            jsonObject = JSONObject.parseObject(additionInfo);
            String globalTraceNumber = jsonObject.getString(ShareParamConstant.GLOBAL_TRACE_NUMBER);
            String esbSeqNumber = jsonObject.getString(ShareParamConstant.ESB_SEQUENCE_NUMBER);
            String globalPathNumber = jsonObject.getString(ShareParamConstant.GLOBAL_PATH_NUMBER);
            //处理上送了全局流水号的
            if (!StringUtil.isEmptyOrNull(globalTraceNumber)) {
                xmlTreeUtil.setXmlTreeStringValue(ShareParamConstant.GLOBAL_TRACE_NUMBER, globalTraceNumber);
            } else {
                if (!StringUtil.isEmptyOrNull(esbSeqNumber)) {
                    xmlTreeUtil.setXmlTreeStringValue(ShareParamConstant.ESB_SEQUENCE_NUMBER, esbSeqNumber);
                }
            }
            if (!StringUtil.isEmptyOrNull(globalPathNumber)) {
                xmlTreeUtil.setXmlTreeStringValue(ShareParamConstant.GLOBAL_PATH_NUMBER, globalPathNumber);
            }
            if (StringUtil.isEmptyOrNull(jsonObject.getString("branchId"))) {
                jsonObject.put("branchId", instId + "777777");
            }
        } else {
            jsonObject = new JSONObject();
            jsonObject.put("branchId", instId + "777777");
        }
        //添加平台商户结算信息
        if (xmlTreeUtil.isExist("platMchntSettleAcctName")) {
            jsonObject.put("platMchntSettleAcctName", xmlTreeUtil.getXmlTreeStringValue("platMchntSettleAcctName"));
        }
        if (xmlTreeUtil.isExist("platMchntSettleAcctNo")) {
            jsonObject.put("platMchntSettleAcctNo", xmlTreeUtil.getXmlTreeStringValue("platMchntSettleAcctNo"));
        }
        if (xmlTreeUtil.isExist("platMchntSettleAcctType")) {
            jsonObject.put("platMchntSettleAcctType", xmlTreeUtil.getXmlTreeStringValue("platMchntSettleAcctType"));
        }
        if (xmlTreeUtil.isExist("platMchntSettleCycle")) {
            jsonObject.put("platMchntSettleCycle", xmlTreeUtil.getXmlTreeStringValue("platMchntSettleCycle"));
        }
        if (xmlTreeUtil.isExist("platMchntSettleType")) {
            jsonObject.put("platMchntSettleType", xmlTreeUtil.getXmlTreeStringValue("platMchntSettleType"));
        }
        return jsonObject.toJSONString();
    }
}
